use yew::Callback;
use crate::business::websocket::dto::common_dto::{ExceptionResp, OkListResp, OkResp};
use crate::business::websocket::dto::im_base_dto::{CollectItemList, CollectPullReq, ConfigItemList, ConfigPullReq, FeedbackItemList, FeedbackPullReq, LabelInfoItemList, LabelPullReq, ModuleInfoItemList, ModulePullReq, SensitiveWordItemList, SensitiveWordPullReq};
use crate::business::websocket::dto::im_department_dto::{DepartmentInfoItemList, DepartmentInfoListReq};
use crate::business::websocket::dto::im_group_dto::{GroupAddReq, GroupCreateReq, GroupInfoItem, GroupInfoItemList, GroupInfoListReq, GroupInfoReq};
use crate::business::websocket::dto::im_session_dto::{SessionItemList, SessionPullReq};
use crate::business::websocket::dto::im_message_dto::{MessageActionList, MessageObject, MessageObjectList, MessageObjectsReq, MessagePullActionOkReq, MessagePullActionReq, MessagePullByMessageIdsReq, MessagePullBySessionIdsReq, MessageReadReq, MessageWithdrawReq, OkListReq};
use crate::business::websocket::dto::ping_dto::{PingReq, PongResp};
use crate::business::websocket::dto::user_dto::{LoginReq, LoginResp, LoginTokenReq, Reconnection, UserInfoItemList, UserInfoListReq};
use crate::business::websocket::receive_send_handle::ImReceiveSendHandle;
use crate::business::websocket::send_data::ImSendData;

pub enum RpcRequest {
    Login(LoginReq),
    LoginByToken(LoginTokenReq),
    Reconnection(Reconnection),
    Ping(PingReq),

    PullConfigs(ConfigPullReq),
    PullDepartments(DepartmentInfoListReq),
    PullUsers(UserInfoListReq),
    PullGroups(GroupInfoListReq),
    PullModules(ModulePullReq),
    PullSessions(SessionPullReq),
    PullMessageBySessionIds(MessagePullBySessionIdsReq),
    PullMessageByServerIds(MessagePullByMessageIdsReq),
    OkMessages(OkListReq),
    PullMessageAction(MessagePullActionReq),
    OkMessageAction(MessagePullActionOkReq),
    PullLabes(LabelPullReq),
    PullCollects(CollectPullReq),
    PullFeedbacks(FeedbackPullReq),
    PullSensitiveWords(SensitiveWordPullReq),

    // 发送消息
    SendMessage(MessageObject),
    SendMessages(MessageObjectsReq),
    ReadMessage(MessageReadReq),
    WithdrawMessage(MessageWithdrawReq),
    GroupCreate(GroupCreateReq),
    GroupAdd(GroupAddReq),
    GroupPullInfo(GroupInfoReq),
}

#[derive(Debug)]
pub enum RpcResponse {
    LoginResponse(Result<LoginResp, ExceptionResp>),
    PongResponse(Result<PongResp, ExceptionResp>),

    PullConfigsResponse(Result<ConfigItemList, ExceptionResp>),
    PullDepartmentsResponse(Result<DepartmentInfoItemList, ExceptionResp>),
    PullUsersResponse(Result<UserInfoItemList, ExceptionResp>),
    PullGroupsResponse(Result<GroupInfoItemList, ExceptionResp>),
    PullModulesResponse(Result<ModuleInfoItemList, ExceptionResp>),
    PullSessionsResponse(Result<SessionItemList, ExceptionResp>),
    PullMessageBySessionIdsResponse(Result<MessageObjectList, ExceptionResp>),
    PullMessageByServerIdsResponse(Result<MessageObjectList, ExceptionResp>),
    OkMessagesResponse(Result<OkResp, ExceptionResp>),
    PullMessageActionResponse(Result<MessageActionList, ExceptionResp>),
    OkMessageActionResponse(Result<OkResp, ExceptionResp>),
    PullLabesResponse(Result<LabelInfoItemList, ExceptionResp>),
    PullCollectsResponse(Result<CollectItemList, ExceptionResp>),
    PullFeedbacksResponse(Result<FeedbackItemList, ExceptionResp>),
    PullSensitiveWordsResponse(Result<SensitiveWordItemList, ExceptionResp>),

    // 发送消息
    SendMessageResponse(Result<OkResp, ExceptionResp>),
    SendMessagesResponse(Result<OkListResp, ExceptionResp>),
    ReadMessageResponse(Result<OkResp, ExceptionResp>),
    WithdrawMessageResponse(Result<OkResp, ExceptionResp>),
    GroupCreateResponse(Result<OkResp, ExceptionResp>),
    GroupAddResponse(Result<OkResp, ExceptionResp>),
    GroupPullInfoResponse(Result<GroupInfoItem, ExceptionResp>),
}

impl RpcRequest {

    pub fn send_data(self, im_send_data: &ImSendData, cb: Callback<RpcResponse>) {
        match self {
            RpcRequest::Login(login_req) => {
                im_send_data.send_data("login".to_string(), login_req, cb);
            }
            RpcRequest::Reconnection(reconnection) => {
                im_send_data.send_data("reconnection".to_string(), reconnection, cb);
            }
            RpcRequest::LoginByToken(login_token) => {
                im_send_data.send_data("loginByToken".to_string(), login_token, cb);
            }
            RpcRequest::Ping(ping_req) => {
                im_send_data.send_data("pingToServer".to_string(), ping_req, cb);
            }

            RpcRequest::PullConfigs(config_pull_req) => {
                im_send_data.send_data("pullConfigs".to_string(), config_pull_req, cb);
            }
            RpcRequest::PullDepartments(config_pull_req) => {
                im_send_data.send_data("pullDepartments".to_string(), config_pull_req, cb);
            }
            RpcRequest::PullUsers(user_info_list_req) => {
                im_send_data.send_data("pullUsers".to_string(), user_info_list_req, cb);
            }
            RpcRequest::PullGroups(group_info_list_req) => {
                im_send_data.send_data("pullGroups".to_string(), group_info_list_req, cb);
            }
            RpcRequest::PullModules(module_pull_req) => {
                im_send_data.send_data("pullModules".to_string(), module_pull_req, cb);
            }
            RpcRequest::PullSessions(pull_session_req) => {
                im_send_data.send_data("pullSessions".to_string(), pull_session_req, cb);
            }
            RpcRequest::PullMessageBySessionIds(pull_message_by_session_ids) => {
                im_send_data.send_data("pullMessageBySessionIds".to_string(), pull_message_by_session_ids, cb);
            }
            RpcRequest::PullMessageByServerIds(pull_message_by_message_ids) => {
                im_send_data.send_data("pullMessageByMessageIds".to_string(), pull_message_by_message_ids, cb);
            }
            RpcRequest::OkMessages(ok_list_req) => {
                im_send_data.send_data("okMessages".to_string(), ok_list_req, cb);
            }
            RpcRequest::PullMessageAction(message_pull_action_req) => {
                im_send_data.send_data("pullActionMessages".to_string(), message_pull_action_req, cb);
            }
            RpcRequest::OkMessageAction(message_pull_action_ok_req) => {
                im_send_data.send_data("okPullActionMessages".to_string(), message_pull_action_ok_req, cb);
            }
            RpcRequest::PullLabes(label_pull_req) => {
                im_send_data.send_data("pullLabels".to_string(), label_pull_req, cb);
            }
            RpcRequest::PullCollects(collect_pull_req) => {
                im_send_data.send_data("pullCollects".to_string(), collect_pull_req, cb);
            }
            RpcRequest::PullFeedbacks(feedback_pull_req) => {
                im_send_data.send_data("pullFeedbacks".to_string(), feedback_pull_req, cb);
            }
            RpcRequest::PullSensitiveWords(sensitive_word_pull_req) => {
                im_send_data.send_data("pullSensitiveWords".to_string(), sensitive_word_pull_req, cb);
            }


            RpcRequest::SendMessage(message_object) => {
                im_send_data.send_data("imMessage".to_string(), message_object, cb);
            }
            RpcRequest::SendMessages(message_objects_req) => {
                im_send_data.send_data("imMessages".to_string(), message_objects_req, cb);
            }
            RpcRequest::ReadMessage(message_read_req) => {
                im_send_data.send_data("readMessages".to_string(), message_read_req, cb);
            }
            RpcRequest::WithdrawMessage(message_read_req) => {
                im_send_data.send_data("withdrawMessages".to_string(), message_read_req, cb);
            }
            RpcRequest::GroupCreate(message_read_req) => {
                im_send_data.send_data("groupCreate".to_string(), message_read_req, cb);
            }
            RpcRequest::GroupAdd(message_read_req) => {
                im_send_data.send_data("groupAdd".to_string(), message_read_req, cb);
            }
            RpcRequest::GroupPullInfo(message_read_req) => {
                im_send_data.send_data("groupInfo".to_string(), message_read_req, cb);
            }
        }
    }

    pub fn send_data2(self, im_send_data: &ImReceiveSendHandle, cb: Callback<RpcResponse>) {
        match self {
            RpcRequest::Login(login_req) => {
                im_send_data.send_data("login".to_string(), login_req, cb);
            }
            RpcRequest::Reconnection(reconnection) => {
                im_send_data.send_data("reconnection".to_string(), reconnection, cb);
            }
            RpcRequest::LoginByToken(login_token) => {
                im_send_data.send_data("loginByToken".to_string(), login_token, cb);
            }
            RpcRequest::Ping(ping_req) => {
                im_send_data.send_data("pingToServer".to_string(), ping_req, cb);
            }

            RpcRequest::PullConfigs(config_pull_req) => {
                im_send_data.send_data("pullConfigs".to_string(), config_pull_req, cb);
            }
            RpcRequest::PullDepartments(department_info_list_req) => {
                log::info!("send pull departments");
                im_send_data.send_data("pullDepartments".to_string(), department_info_list_req, cb);
            }
            RpcRequest::PullUsers(user_info_list_req) => {
                im_send_data.send_data("pullUsers".to_string(), user_info_list_req, cb);
            }
            RpcRequest::PullGroups(group_info_list_req) => {
                im_send_data.send_data("pullGroups".to_string(), group_info_list_req, cb);
            }
            RpcRequest::PullModules(module_pull_req) => {
                im_send_data.send_data("pullModules".to_string(), module_pull_req, cb);
            }
            RpcRequest::PullSessions(pull_session_req) => {
                im_send_data.send_data("pullSessions".to_string(), pull_session_req, cb);
            }
            RpcRequest::PullMessageBySessionIds(pull_message_by_session_ids) => {
                im_send_data.send_data("pullMessageBySessionIds".to_string(), pull_message_by_session_ids, cb);
            }
            RpcRequest::PullMessageByServerIds(pull_message_by_message_ids) => {
                im_send_data.send_data("pullMessageByMessageIds".to_string(), pull_message_by_message_ids, cb);
            }
            RpcRequest::OkMessages(ok_list_req) => {
                im_send_data.send_data("okMessages".to_string(), ok_list_req, cb);
            }
            RpcRequest::PullMessageAction(message_pull_action_req) => {
                im_send_data.send_data("pullActionMessages".to_string(), message_pull_action_req, cb);
            }
            RpcRequest::OkMessageAction(message_pull_action_ok_req) => {
                im_send_data.send_data("okPullActionMessages".to_string(), message_pull_action_ok_req, cb);
            }
            RpcRequest::PullLabes(label_pull_req) => {
                im_send_data.send_data("pullLabels".to_string(), label_pull_req, cb);
            }
            RpcRequest::PullCollects(collect_pull_req) => {
                im_send_data.send_data("pullCollects".to_string(), collect_pull_req, cb);
            }
            RpcRequest::PullFeedbacks(feedback_pull_req) => {
                im_send_data.send_data("pullFeedbacks".to_string(), feedback_pull_req, cb);
            }
            RpcRequest::PullSensitiveWords(sensitive_word_pull_req) => {
                im_send_data.send_data("pullSensitiveWords".to_string(), sensitive_word_pull_req, cb);
            }


            RpcRequest::SendMessage(message_object) => {
                im_send_data.send_data("imMessage".to_string(), message_object, cb);
            }
            RpcRequest::SendMessages(message_objects_req) => {
                im_send_data.send_data("imMessages".to_string(), message_objects_req, cb);
            }
            RpcRequest::ReadMessage(message_read_req) => {
                im_send_data.send_data("readMessages".to_string(), message_read_req, cb);
            }
            RpcRequest::WithdrawMessage(message_read_req) => {
                im_send_data.send_data("withdrawMessages".to_string(), message_read_req, cb);
            }
            RpcRequest::GroupCreate(message_read_req) => {
                im_send_data.send_data("groupCreate".to_string(), message_read_req, cb);
            }
            RpcRequest::GroupAdd(message_read_req) => {
                im_send_data.send_data("groupAdd".to_string(), message_read_req, cb);
            }
            RpcRequest::GroupPullInfo(message_read_req) => {
                im_send_data.send_data("groupInfo".to_string(), message_read_req, cb);
            }
        }
    }

}